<template>
   <div>
    <h1>{{startAndEndNumber}}</h1>
    <div class="pagination">
      <button @click="$emit('currentPage',pageNo - 1)" :disabled="pageNo==1">上一页</button>
      <!-- 时而显示时而隐藏 -->
      <button v-if="startAndEndNumber.start > 1 " @click="$emit('currentPage',1)">1</button>
      <button v-if="startAndEndNumber.start > 2 ">...</button>

      <button   :class="{active:pageNo==page}" v-for="(page,index) in startAndEndNumber.end" v-if="page>=startAndEndNumber.start" @click="$emit('currentPage',page)">{{page}}</button>
  
      <button v-if="startAndEndNumber.end < totalPage - 1">...</button>
      <button v-if="startAndEndNumber.end < totalPage" @click="$emit('currentPage',totalPage)">{{ totalPage }}</button>
      <button @click="$emit('currentPage',pageNo + 1)" :disabled="pageNo==totalPage">下一页</button>

      <select class="cur" v-model="currentPageSize" @change="$emit('sizeChange',currentPageSize)">
          <option v-for="(item,index) in pageSizes" :key="index" :value="item">{{item}}条/页</option>
      </select>
      <button style="margin-left: 30px">共 {{ total }} 条</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  data() {
    return {
      currentPageSize:5
    }
  },
  props: ["total", "pageSize", "pageNo", "pager"],
  computed: {
    //分页器一共多少页算出来
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    //计算出分页器连续页码起始与结束数字范围，将来在中间部分展示
    //当前pageNo 6   totalPage:20  pager:5
    startAndEndNumber() {
      //计算出分页器练习几个数字范围
      let start = 0,
        end = 0;
      const { totalPage, pager, pageNo } = this;
      //特殊情况情况:总页码比连续页码都小----起始与结束数字是谁
      if (totalPage < pager) {
        start = 1;
        end = totalPage;
      } else {
        //总页码大于等于连续页码-才有可能出现连续五个
        start = pageNo - parseInt(pager / 2);
        end = pageNo + parseInt(pager / 2);
        //start|end:可能出现负数|大于总页数，将来约束判断范围
         //约束左边计算start与end范围
         if (start < 1) {
          start = 1;
          end = pager;
        }
        //约束右边计算start与end范围
        if (end > totalPage) {
          start = totalPage - pager + 1;
          end = totalPage;
      }
    }
      return{ start, end };
    },
  },  
  methods: {
    handler(){
      alert(123);
    }
  },        
};                 
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
  .cur{
     width: 150px;
     height: 30px;
     text-align: center;
  }
}
</style>